Practice Problems and Solutions 2 B

These problems are offered to help understand the lecture material and assist with completing homeworks; they will also form the basis for some problems on the midterms. They are entirely optional, and feel free to skip if you understand a concept!

You should do these problems on paper or mentally first, and only THEN check the solution.

This set of problems uses Bare-Bones Haskell, with explicit data declarations for Pairs, Triples, and Lists; here is a version of the same problems with the built-in Haskell notations for tuples and lists.

 

2. BareBones Haskell: Polymorphic Types and Type Checking

Suppose we have the following data declarations:

data Pair a b = P a b
data Triple a b c = T a b c
data List a = Nil | Cons a (List a)

data Either a b = Left a | Right b 
data Maybe a = Nothing | Just a 


 

We assume that we can have Integer and Bools as defined in the Haskell Prelude. Assume for the purposes of these exercises that all arithmetic operators operate only on Integers, so pretend the type of (+) is:

 (+): Integer -> Integer -> Integer.
Therefore, all numeric types will simply be assumed to be Integer.

Give the type of the following (polymorphic) functions. Note: Assume that any numeric type is simply Integer. Also, use type variables starting with a, b, c, etc. There are no errors in these functions, all can be given a polymorphic type.

2.1.

id x = x

Show Solution

2.2.

select x y = x

Show Solution

2.3.

makePair x y = P x y

Show Solution

2.4.

makeSingleton x = Cons x Nil

Show Solution

2.5.

two x = P x x

Show Solution

2.6.

triple2Pair (T x y z) = P x (P y z)

Show Solution

2.7.

pair2List (P x y) = Cons x (Cons y Nil

Show Solution

2.8.

list2Pairs (Cons w (Cons x (Cons y (Cons z Nil)))) = (P (P w x) (P y z))

Show Solution

2.9.

safeSecond (Cons _ (Cons x _)) = Just x
safeSecond _                   = Nothing

Show Solution

2.10.

checkedSecond (Cons _ (Cons x _)) = Right x
checkedSecond _                   = Left False

Show Solution

2.11.

weirdSecond (Cons _ (Cons x _)) = Right (Just x)
weirdSecond _                   = Left Nothing

Show Solution

2.12.

strangeSecond (Cons _ (Cons x _)) = Just (Right x)
strangeSecond _                   = Nothing

Show Solution

2.13.

bizarreHead Nil               = Nothing
bizarreHead (Cons (Just x) _) = Just (Just x)

Show Solution

2.14.

repeat1 x 0 = Nil
repeat1 x n = (Cons x (repeat1 x (n-1)))

Show Solution

2.15.

repeat2 x y 0 = Nil
repeat2 x y n = (Cons x (Cons y (repeat2 x y (n-1))))

Show Solution

2.16.

repeatPairs x y 0 = Nil
repeatPairs x y n = (Cons (P x y) (repeatPairs x y (n-1))

Show Solution

2.17.

try1 (P x y) z = (P x (P y z))

Show Solution

2.18.

try2 (T x y z) = (P x (y z))

Show Solution

2.19.

try3 x y =  x ( (<4) y 

Show Solution

2.20.

try4 x y z = x ((*3) y) ((`mod` z) 2)

Show Solution

2.21.

apply1 x y = x y

Show Solution

2.22.

apply2 x y = x (x y)

Show Solution

2.23.

apply3 x y z = (x y) z

Show Solution

2.24.

apply4 x y z = x (y z)

Show Solution

2.25.

apply5 x y z = x y z

Show Solution

2.26.

apply6 x y z = (x z) (y z)

Show Solution

2.27.

apply7 w x y z = w (x (y z))

Show Solution

2.28.

apply8 w x y z = ((w x) y) z

Show Solution

2.29.

apply9 w x y z = w x y z

Show Solution

2.30.

switch1 x y = switch1 y x    -- will not terminate, but will type check!

Show Solution

2.31.

switch2 x y z = switch2 y x z

Show Solution

2.32.

switch3 x y z = switch3 z y x

Show Solution

2.33.

switch4 x y z = switch4 z x y

Show Solution

2.34.

lam1 = \x -> x

Show Solution

2.35.

lam2 = \x -> (P x x)

Show Solution

2.36.

lam3 = \x -> (T x 4 x)

Show Solution

2.37.

lam4 = \_ -> Nothing    -- You can use an anonymous variable in lambda expression!

Show Solution

2.38.

lam5 = \x -> (P (Left x), (Just x))

Show Solution

2.39.

lam6 = \x y -> y x

Show Solution

2.40.

lam7 = \x -> \y -> y x

Show Solution

2.41.

lam8 = \x -> \y -> f x y
          where f x = \x y -> (P x x)

Show Solution

2.42.

lam9 x = (\y -> x y)

Show Solution

2.43.

lam10 x y = (\z -> x (P y z))

Show Solution

2.44.

test1 = \x y -> ((\z -> x z) y)

Show Solution

2.45.

test2 x = \y -> ((\z -> x z) y)

Show Solution

2.46.

test3 x y =  (x . y) 5

Show Solution

2.47.

test4 x y = ( (*3) . x) y

Show Solution

2.48.

test5 x y = (x . (*3)) y

Show Solution

2.49.

test6 = \x -> ((\y z -> y z) x 3)

Show Solution

2.50.

test7 x y = x . (`f` True)
              where f x y = (P x y)

Show Solution

End of Practice Problems 2 B.